﻿Public Class frmFacturaBuscador

    Private _idCaja As Integer

    Public Property idCaja() As Integer
        Get
            Return _idCaja
        End Get
        Set(ByVal value As Integer)
            _idCaja = value
        End Set
    End Property

    Private _feInicio As DateTime

    Public Property feInicio() As DateTime
        Get
            Return _feInicio
        End Get
        Set(ByVal value As DateTime)
            _feInicio = value
        End Set
    End Property

    Private _feFin As DateTime

    Public Property feFin() As DateTime
        Get
            Return _feFin
        End Get
        Set(ByVal value As DateTime)
            _feFin = value
        End Set
    End Property

    Private Sub frmClienteBuscador_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If esContado Or esPago Then
            fechaInicio.Value = feInicio
            fechaFin.Value = feFin
        Else
            fechaInicio.Value = DateTime.Now.AddDays(-90)
        End If

        dgVentas.AutoGenerateColumns = False
        dgCompras.AutoGenerateColumns = False

        If esCompra Then
            dgCompras.Visible = True
            dgVentas.Visible = False
            CargarCompras()
        Else
            dgCompras.Visible = False
            dgVentas.Visible = True
            CargarVentas()
        End If

    End Sub

    Private _codCliente As Integer = 0

    Public Property codCliente() As Integer
        Get
            Return _codCliente
        End Get
        Set(ByVal value As Integer)
            _codCliente = value
        End Set
    End Property

    Private _codVenta As Integer = 0

    Public Property codVenta() As Integer
        Get
            Return _codVenta
        End Get
        Set(ByVal value As Integer)
            _codVenta = value
        End Set
    End Property

    Private _codCompra As Integer = 0

    Public Property codCompra() As Integer
        Get
            Return _codCompra
        End Get
        Set(ByVal value As Integer)
            _codCompra = value
        End Set
    End Property

    Private _esDevolucion As Boolean = False

    Public Property esDevolucion() As Boolean
        Get
            Return _esDevolucion
        End Get
        Set(ByVal value As Boolean)
            _esDevolucion = value
        End Set
    End Property

    Private _conSaldo As Boolean = False

    Public Property conSaldo() As Boolean
        Get
            Return _conSaldo
        End Get
        Set(ByVal value As Boolean)
            _conSaldo = value
        End Set
    End Property

    Private _esCompra As Boolean = False

    Public Property esCompra() As Boolean
        Get
            Return _esCompra
        End Get
        Set(ByVal value As Boolean)
            _esCompra = value
        End Set
    End Property

    Private _esOP As Boolean = False

    Public Property esOP() As Boolean
        Get
            Return _esOP
        End Get
        Set(ByVal value As Boolean)
            _esOP = value
        End Set
    End Property

    Private _esContado As Boolean = False

    Public Property esContado() As Boolean
        Get
            Return _esContado
        End Get
        Set(ByVal value As Boolean)
            _esContado = value
        End Set
    End Property

    Private _esPago As Boolean = False

    Public Property esPago() As Boolean
        Get
            Return _esPago
        End Get
        Set(ByVal value As Boolean)
            _esPago = value
        End Set
    End Property

    Private _Cliente As String = ""

    Public Property ClienteDesc() As String
        Get
            Return _Cliente
        End Get
        Set(ByVal value As String)
            _Cliente = value
        End Set
    End Property

    Dim controller As New ControllerSigeco.controller_ventas()
    Dim controllerCompra As New ControllerSigeco.controller_compras()

    Private Sub CargarVentas()

        Dim dt As New DataTable

        If esDevolucion Then
            dt = controller.get_Ventas_Devolucion(oiml.dbConn, codCliente, txtNroFactura.Text, fechaInicio.Value, fechaFin.Value)
            dgVentas.DataSource = dt

        ElseIf esContado Then
            dt = controller.get_Ventas_Contado_All(oiml.dbConn, 0, fechaInicio.Value, fechaFin.Value, "CONTADO", "", "", txtNroFactura.Text, "", codCliente)
            dgVentas.DataSource = dt

        Else
            dt = controller.get_Ventas_All(oiml.dbConn, 0, fechaInicio.Value, fechaFin.Value, "", "", "", txtNroFactura.Text, "", codCliente, conSaldo, "NO")
            dgVentas.DataSource = dt

        End If

    End Sub

    Private Sub CargarCompras()

        Dim dt As New DataTable

        If esDevolucion Then
            dt = controllerCompra.get_Compras_Devolucion(oiml.dbConn, codCliente, txtNroFactura.Text, fechaInicio.Value, fechaFin.Value)
            dgCompras.DataSource = dt

        ElseIf esPago Then
            dt = controllerCompra.get_Compras_Contado_All(oiml.dbConn, 0, fechaInicio.Value, fechaFin.Value, "CONTADO", "", "", txtNroFactura.Text, "", codCliente)
            dgCompras.DataSource = dt

        Else

            If esOP Then
                dt = controllerCompra.get_Compras_All(oiml.dbConn, 0, fechaInicio.Value, fechaFin.Value, "", "", "", txtNroFactura.Text, "", codCliente, conSaldo, "")
            Else
                dt = controllerCompra.get_Compras_All(oiml.dbConn, 0, fechaInicio.Value, fechaFin.Value, "", "", "", txtNroFactura.Text, "", codCliente, conSaldo, "NO")
            End If

            dgCompras.DataSource = dt

        End If

    End Sub

    Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click

        Me.DialogResult = Windows.Forms.DialogResult.Cancel
        Me.Close()

    End Sub

    Private m_frmRegistroCobro As frmVentasCobro
    Public Property frmRegistroCobro() As frmVentasCobro
        Get
            Return m_frmRegistroCobro
        End Get
        Set(ByVal value As frmVentasCobro)
            m_frmRegistroCobro = value
        End Set
    End Property

    Private m_frmRegistroPago As frmComprasCobro
    Public Property frmRegistroPago() As frmComprasCobro
        Get
            Return m_frmRegistroPago
        End Get
        Set(ByVal value As frmComprasCobro)
            m_frmRegistroPago = value
        End Set
    End Property

    Private Sub seleccionar_Cliente()
        Try

            If esPago Then

                codCompra = dgCompras.SelectedRows(0).Cells(0).Value

                If codCompra > 0 Then

                    frmRegistroPago = New frmComprasCobro
                    frmRegistroPago.codCompra = codCompra
                    frmRegistroPago.idCaja = idCaja
                    frmRegistroPago.codProveedor = codCliente
                    frmRegistroPago.ShowDialog()
                    CargarCompras()

                End If

            ElseIf esCompra Then

                If dgCompras.SelectedRows.Count > 0 Then

                    codCompra = dgCompras.SelectedRows(0).Cells(0).Value
                    'ClienteDesc = dgCliente.SelectedRows(0).Cells(1).Value
                    Me.DialogResult = Windows.Forms.DialogResult.OK
                    Me.Close()

                End If

            ElseIf esContado Then

                codVenta = dgVentas.SelectedRows(0).Cells(0).Value

                If codVenta > 0 Then

                    frmRegistroCobro = New frmVentasCobro
                    frmRegistroCobro.codVenta = codVenta
                    frmRegistroCobro.idCaja = idCaja
                    frmRegistroCobro.codCliente = codCliente
                    frmRegistroCobro.ShowDialog()
                    CargarVentas()

                End If

            Else

                If dgVentas.SelectedRows.Count > 0 Then

                    codVenta = dgVentas.SelectedRows(0).Cells(0).Value
                    'ClienteDesc = dgCliente.SelectedRows(0).Cells(1).Value
                    Me.DialogResult = Windows.Forms.DialogResult.OK
                    Me.Close()

                End If

            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

    Private Sub txtBuscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBuscar.Click
        If esCompra Then
            CargarCompras()
        Else
            CargarVentas()
        End If
    End Sub

    Private Sub txtProveedor_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtNroFactura.KeyDown, fechaInicio.KeyDown, fechaFin.KeyDown
        If e.KeyCode = Keys.Enter Then
            If esCompra Then
                CargarCompras()
            Else
                CargarVentas()
            End If


        End If
    End Sub

    Private Sub dgCliente_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgVentas.DoubleClick, dgCompras.DoubleClick
        seleccionar_Cliente()
    End Sub

    Private Sub dgCliente_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dgVentas.KeyDown, dgCompras.KeyDown
        If e.KeyCode = Keys.Enter Then
            seleccionar_Cliente()
        End If

    End Sub

    Private Sub dgCompras_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgCompras.CellContentClick

    End Sub
End Class